package com.yf.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.yf.dto.Page;
import com.yf.entity.Department;
import com.yf.mapper.DepartmentMapper;
import com.yf.service.DepartmentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@Transactional
public class DepartmentServiceImpl implements DepartmentService {

    @Autowired
    private DepartmentMapper departmentMapper;

    @Override
    public void addDepartment(Department department) {
        departmentMapper.addDepartment(department);
    }

    @Override
    public void deleteDepartment(Integer deptId) {
        departmentMapper.deleteDepartment(deptId);
    }

    @Override
    public void updateDepartment(Department department) {
        departmentMapper.updateDepartment(department);
    }

    @Override
    public Department getDepartmentByDeptId(Integer deptId) {
        return departmentMapper.getDepartmentByDeptId(deptId);
    }

    @Override
    public Page getDepartmentPage(Integer pageNo, Integer pageSize) {
        Page page = new Page();

        PageHelper.startPage(pageNo,pageSize);
        List<Department> departmentList = departmentMapper.getDepartmentList();
        PageInfo<Department> info = new PageInfo<>(departmentList);

        page.setPageNo(info.getPageNum());
        page.setPageSize(info.getPageSize());
        page.setPageCount(info.getPages());
        page.setCount((int) info.getTotal());
        page.setHasNext(info.isHasNextPage());
        page.setHasPre(info.isHasPreviousPage());
        page.setData(info.getList());

        return page;
    }
}
